package com.sz.biz.common;

import com.sz.biz.common.AbstractBizService;
import com.sz.common.base.dao.ext.VersionEntityMapperDao;
import com.sz.common.base.entity.VersionEntity;
import com.sz.common.core.service.PrincipalUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.List;

/**
 * Function: 处理通用的一些逻辑. <br>
 * Date: 2017年03月07日 17:03 <br>
 * Author: wentao.chang
 */
public abstract class AbstractVersionEntityService extends AbstractBizService {

    @Autowired
    public VersionEntityMapperDao versionEntityMapperDao;

    /**
     * 新增
     *
     * @param entity 新增的实体
     * @return
     */
    public int save(VersionEntity entity) throws ParseException {
        entity.setLastUpdateUserId(PrincipalUtils.getAccountId());
        if (entity.getCreateUserId() == null || entity.getCreateUserId() == 0) {
            entity.setCreateUserId(PrincipalUtils.getAccountId());
        }
        versionEntityMapperDao.insertEntity(getMapperNamespace() + ".insert", entity);
        return entity.getId();
    }

    public void updateVersionEntity(VersionEntity entity) {
        entity.setLastUpdateUserId(PrincipalUtils.getAccountId());
        if (entity.getCreateUserId() == null || entity.getCreateUserId() == 0) {
            entity.setCreateUserId(PrincipalUtils.getAccountId());
        }
        versionEntityMapperDao.updateEntity(getMapperNamespace() + ".update", getMapperNamespace() + ".insert", entity);
    }

    public void deleteEntity(String uid) {
        versionEntityMapperDao.deleteEntity(getMapperNamespace() + ".update",
                getMapperNamespace() + ".softDelete", uid);
    }

    public void deleteEntities(List<String> uids) {
        versionEntityMapperDao.deleteEntities(getMapperNamespace() + ".update",
                getMapperNamespace() + ".softDelete", uids);
    }

}
